#include <iostream>

int main(int argc, char* argv[])
{
	int count = 0;

	FILE* hRead = 0;
	FILE* hWrite = 0;

	unsigned char c = 0;
	char writeFileAddr[2048]{};

	if (argc != 2)
	{
		printf_s("ERROR: NEED A FILE.\n");
		system("pause");
		return 0;
	}

	sprintf_s(writeFileAddr, 2048, "%s.hex", argv[1]);
	fopen_s(&hRead, argv[1], "rb");
	fopen_s(&hWrite, writeFileAddr, "w");

	if (hRead == nullptr || hWrite == nullptr)
	{
        if(hRead)
            fclose(hRead);
        if(hWrite)
            fclose(hWrite);            
		printf_s("ERROR: FAILED TO OPEN FILE.\n");
		system("pause");
		return 0;
	}

	while (true)
	{
		c = fgetc(hRead);
		if (feof(hRead))
		{
			break;
		}

		fprintf_s(hWrite, "%02X ", c);
		
		if (++count >= 16)
		{
			fprintf_s(hWrite, "\n");
			count = 0;
		}
	}
    fclose(hRead)；
    fclose(hWrite);

	return 0;
}
